/**
 * Cted by json on 2016/9/6.
 */
//公共提示框
dwc.CommonAlertView = cc.Node.extend({
    ctor:function(){
        this._super();
        this.width = cc.winSize.width;
        this.height = cc.winSize.height;
        this.setPosition(cc.winSize.width/2,cc.winSize.height/2);
        this.setAnchorPoint(0.5,0.5);
        this.setVisible(false);
        //黑色背景层
        this.m_layerColor = new cc.LayerColor(cc.color(0,0,0,0), cc.winSize.width, cc.winSize.height);
        this.m_layerColor.ignoreAnchor = false;
        this.m_layerColor.anchorX = 0.5;
        this.m_layerColor.anchorY = 0.5;
        this.m_layerColor.x = this.width/2;
        this.m_layerColor.y = this.height/2;
        this.addChild(this.m_layerColor);

        return true;
    },

    onEnter : function(){
        this._super();

        var that = this;
        //1.添加触摸监听,屏蔽触摸,当前层以下的UI都不能被点击
        cc.eventManager.addListener({
            event : cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches : true,
            onTouchBegan : function(touch,event){
                return that.onTouchBegan(touch,event);
            }
        }, this);
    },

    Show:function(bEffect){

        this.stopAllActions();
        this.setVisible(true);
        this.m_layerColor.setOpacity(150);
        if(bEffect)
        {
            this.setScale(0.01);
            this.m_layerColor.setScale(100);
            var action = cc.scaleTo(0.2, 1).easing(cc.easeInOut(1.0));
            this.runAction(action);

            var action1 = cc.delayTime(0.2);
            var action2 = cc.scaleTo(0.1,1);
            var seq = cc.sequence(action1, action2);
            this.m_layerColor.runAction(seq);
        }
    },
    Close:function(){
        this.stopAllActions();
        this.setVisible(false);
        this.removeFromParent();
    },

    onTouchBegan:function(touch,event){

        return true;
    }

});

//公共提示框
dwc.PopupAlert=dwc.CommonAlertView.extend({
    ctor: function (_msg,_cb) {
        this._super();
        this._msg = _msg;
        this._cb = _cb;
        this.initUI();
    },
    initUI: function (idx) {
        var that = this;
        var mainNode = ccs.load(dwc.g_arrGCRes.gc_PopupAlert_json).node;
        this.addChild(mainNode);

        var sureBtn=$.getNodeByName(mainNode,"ID_BUTTON_SURE");
        sureBtn.addClickEventListener(function () {
            if(that._cb)
            {
                that._cb();
            }
            that.Close();
        })

        this.giveBack=$.getNodeByName(mainNode,"ID_SPRITE_GIVEBACK");
        this.msgLabel=$.getNodeByName(mainNode,"ID_LABEL_MSG");
        this.msgLabel.setString(this._msg);

    },

});
//新聊天////////////////////////////////////////////////
dwc.GameChat=dwc.CommonAlertView.extend({
    ctor: function (_emoCallBack,_yuYinCallBack,_textCallBack) {
        this._super();
        this.emoCallBack=_emoCallBack;
        this.yuYinCallBack=_yuYinCallBack;
        this.textCallBack=_textCallBack;
        this.ignoreAnchor=false;
        var mainNode=ccs.load(dwc.g_arrGCRes.game_chat_json).node;
        this.addChild(mainNode);
        this.initMain(mainNode);
    },
    initMain: function (mainNode) {
        //表情
        for(var i=1;i<28;i++)
        {
            var emoBtn= $.getNodeByName(mainNode,"IDID_BUTTON_CHATEMO"+i);
            emoBtn.tag=i;
            emoBtn.addClickEventListener(function (sender)
            {
                this.OnEmotionBtnClick(sender);

            }.bind(this));
        }
        //语音
        for(var i=0;i<11;i++)
        {
            var yuYinBtn= $.getNodeByName(mainNode,"ID_BUTTON_CHATYUYIN"+i);
            yuYinBtn.tag=i+1;
            yuYinBtn.addClickEventListener(function (sender)
            {
                this.OnYuYinBtnClick(sender);

            }.bind(this));
        }
        //输入框
        var sendBtn=$.getNodeByName(mainNode,"ID_BUTTON_SEND");
        sendBtn.addClickEventListener(function ()
        {
            this.OnSendBtnClick();

        }.bind(this));
        //输入框
        var inputBack=$.getNodeByName(mainNode,"ID_SPRITE_INPUTBG");
        var scale9=new cc.Scale9Sprite("newchat_small_bg1.png");
        this.inputEditBox = new cc.EditBox(cc.size(670, 60),scale9);
        this.inputEditBox.setDelegate(this);
        this.inputEditBox.setPlaceHolder("请输入聊天内容");
        this.inputEditBox.setPlaceholderFontSize(24);
        this.inputEditBox.setPlaceholderFontColor(cc.color.BLACK);
        this.inputEditBox.setFontSize(24);
        this.inputEditBox.setFontColor(cc.color.BLACK);
        this.inputEditBox.setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
        this.inputEditBox.setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
        this.inputEditBox.setAnchorPoint(0,0);
        this.inputEditBox.setPosition(0,0);
        inputBack.addChild(this.inputEditBox);

    },
    OnEmotionBtnClick: function (sender) {
        var emoIdx=sender.tag;
        this.emoCallBack(emoIdx);
        this.removeFromParent();
    },
    OnYuYinBtnClick: function (sender) {
        var yuYinIdx=sender.tag;
        this.yuYinCallBack(yuYinIdx);
        this.removeFromParent();
    },
    OnSendBtnClick: function () {
        if(this.inputEditBox.getString()){
            var msg=this.inputEditBox.string;
            this.textCallBack(msg);
            this.inputEditBox.setString("");
            this.removeFromParent();
        }else {
            cc.log("发送内容不能为空！！！！！");
        }
    },
    //editBox delegate///////////////////////////////////////////////////////////////////////////////////////////////////

    editBoxEditingDidBegin: function (editBox) {

    },

    editBoxEditingDidEnd: function (editBox) {
        //关键字屏蔽
        var textMsg = editBox.getString();
        editBox.setString( dwc.FilterMaskingKey(textMsg));
    },

    editBoxTextChanged: function (editBox, text) {
        //截取输入字符长度
        if(dwc.CalCharOfString(text) > 20){
            text = text.substr(0, 20);
        }
    },

    editBoxReturn: function (editBox) {
        this.editBoxEditingDidEnd(editBox);
    }
});